package site.fantasycube.blog.util;

import site.fantasycube.blog.util.exception.BlogException;

import java.math.BigDecimal;
import java.util.*;

/**
 * 判断是否为空
 * 断言工具
 * @author lisi
 * @version 1.0
 * @date 2020-7-28
 */
public class StringUtils {

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(String value) {
        return null != value && value.length() != 0 && !"null".equals(value);
    }

    /**
     * 判断不为空
     *
     * @param object 需要比较的对象
     * @param value  value
     * @return boolean
     */
    public static boolean isNotNull(Object object, String value) {
        if (null != object) {
            return null != value && value.length() != 0 && !"null".equals(value);
        }
        return false;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Integer value) {
        return null != value;
    }

    /**
     * 判断不为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNotNull(Long value) {
        return null != value && 0L != value;
    }

    /**
     * 判断不为空
     *
     * @param collection 集合
     * @return boolean
     */
    public static boolean isNotNull(Collection<?> collection) {
        return collection != null && collection.size() != 0;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return 结果
     */
    public static boolean isNull(Integer value) {
        return null == value;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return 结果
     */
    public static boolean isNull(long value) {
        return value == 0;
    }

    /**
     * 判断为空
     *
     * @param value value
     * @return boolean
     */
    public static boolean isNull(String value) {
        return value == null || value.length() <= 0;
    }

    /**
     * 判断为空
     *
     * @param bigDecimal BigDecimal
     * @return boolean
     */
    public static boolean isNull(BigDecimal bigDecimal) {
        return BigDecimal.ZERO.equals(bigDecimal);
    }

    /**
     * 判断为空
     *
     * @param collection 集合
     * @return boolean
     */
    public static boolean isNull(Collection<?> collection) {
        return collection == null || collection.size() == 0;
    }

    /**
     * 将字符串转为List，依据为,
     *
     * @return List<String>
     */
    public static List<String> stringConversionList(String res) throws IllegalArgumentException {
        try {
            List<String> array = new ArrayList<>();
            if (isNotNull(res)) {
                String[] dataset = res.split(",");
                array.addAll(Arrays.asList(dataset));
                return array;
            }
        } catch (BlogException e) {
            throw new IllegalArgumentException("参数非法...");
        }
        return Collections.emptyList();
    }

    /**
     * 数组转字符串
     *
     * @param objects 需要转换的对象
     * @return 字符串
     */
    public static String arrayConversionString(Object[] objects) {
        if (objects.length != 0) {
            return Arrays.toString(objects).replace("[", "").replace("]", "");
        }
        return null;
    }

    /**
     * 生成1-100的随机数
     *
     * @return 结果
     */
    public static Integer random() {
        return (int) (Math.random() * (100 - 1) + 1);
    }
}
